package com.xinxing.city.recovery.ui;

import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hikvision.netsdk.NET_DVR_PREVIEWINFO;
import com.xinxing.city.recovery.R;
import com.xinxing.city.recovery.databinding.ActivitySetDeviceHjBinding;
import com.xinxing.city.recovery.entitys.HkCamera;
import com.xinxing.city.recovery.entitys.SetCategory;
import com.xinxing.city.recovery.hk.Hk;
import com.xinxing.city.recovery.hk.SDKGuider;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;

import java.lang.reflect.Type;

public class SetDeviceHkActivity extends AppCompatActivity implements SurfaceHolder.Callback{
    private ActivitySetDeviceHjBinding binding;
    private HkCamera hkCamera = new HkCamera();
    private SurfaceHolder holder;
    private int m_iPreviewHandle = -1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_set_device_hj);
        init();
        binding.setHkCamera(hkCamera);
        holder = binding.HkView.getHolder();
        binding.HkView.getHolder().addCallback(this);
        binding.HkView.setZOrderOnTop(true);
    }
    private void init(){
        String str = (String) SharedPreferencesUtils.getParam(this,"hk_camera","");
        if(!str.equals("")){
            Type dev = new TypeToken<HkCamera>() {}.getType();
            Gson gson = new Gson();;
            hkCamera = gson.fromJson(str,dev);
            Hk hk = new Hk(this,hkCamera.getUserIp(),hkCamera.getUserName(),hkCamera.getUserPassward(),hkCamera.getPort());
            hk.initVideo();
        }
    }
    public void saveSub(View view){
        Gson gson = new Gson();;
        SharedPreferencesUtils.setParam(this,"hk_camera",gson.toJson(hkCamera));
        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
        videoOnstart();
        this.finish();
    }
    //开启摄像头
    private void videoOnstart() {
        binding.HkView.setZOrderOnTop(true);
        SDKGuider.g_sdkGuider.m_comPreviewGuider.RealPlaySurfaceChanged_jni(m_iPreviewHandle, 0, holder);
        NET_DVR_PREVIEWINFO struPlayInfo = new NET_DVR_PREVIEWINFO();
        struPlayInfo.lChannel = 1;
        struPlayInfo.dwStreamType = 0;
        struPlayInfo.bBlocked = 1;
        struPlayInfo.hHwnd = binding.HkView.getHolder();
        m_iPreviewHandle = SDKGuider.g_sdkGuider.m_comPreviewGuider.RealPlay_V40_jni(0,struPlayInfo, null);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SDKGuider.g_sdkGuider.m_comPreviewGuider.RealPlay_Stop_jni(m_iPreviewHandle);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        binding.HkView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        if (-1 == m_iPreviewHandle) {
            return;
        }
        Surface surface = holder.getSurface();
        if (surface.isValid()) {
            if (-1 == SDKGuider.g_sdkGuider.m_comPreviewGuider.RealPlaySurfaceChanged_jni(m_iPreviewHandle, 0, holder))
                Toast.makeText(SetDeviceHkActivity.this, "NET_DVR_PlayBackSurfaceChanged" + SDKGuider.g_sdkGuider.GetLastError_jni(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
        if (-1 == m_iPreviewHandle) {
            return;
        }
        if (holder.getSurface().isValid()) {
            if (-1 == SDKGuider.g_sdkGuider.m_comPreviewGuider.RealPlaySurfaceChanged_jni(m_iPreviewHandle, 0, null)) {
                Toast.makeText(SetDeviceHkActivity.this, "NET_DVR_RealPlaySurfaceChanged" + SDKGuider.g_sdkGuider.GetLastError_jni(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}
